package com.zx.idc.backend.gui.base.serve;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zx.idc.backend.gui.common.BaseApi;
import com.zx.idc.backend.gui.common.Result;
import com.zx.idc.backend.gui.common.base.BaseExcelApi;
import com.zx.idc.backend.gui.common.base.BaseUpdateApi;
import com.zx.idc.common.util.DealObjectNullValue;
import com.zx.idc.ds.base.server.entity.BaseServerInfo;
import com.zx.idc.ds.base.server.service.IBaseServerInfoService;
import com.zx.idc.ds.base.server.vo.BaseServerInfoVo;
import com.zx.idc.ds.common.constant.DelFlagConstant;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 服务器信息管理 前端控制器
 * </p>
 *
 * @author rxliuli
 * @since 2019-01-31
 */
@RestController
@RequestMapping("/api/baseServerInfo")
public class BaseServerInfoController extends BaseApi implements BaseUpdateApi<IBaseServerInfoService, BaseServerInfo>, BaseExcelApi<IBaseServerInfoService, BaseServerInfoVo> {

    @Autowired
    private IBaseServerInfoService iBaseServerInfoService;

    @GetMapping("/query-page")
    @RequiresPermissions("basic-data:base-server-info")
    public Result<Page<BaseServerInfoVo>> page(Page<BaseServerInfoVo> page, BaseServerInfoVo baseServerInfoVo) {
        return success(iBaseServerInfoService.queryPage(page, baseServerInfoVo));
    }

    @GetMapping("/select-list")
    @RequiresPermissions("basic-data:base-server-info")
    public Result<List<BaseServerInfo>> list() {
        return success(iBaseServerInfoService.selectList(new EntityWrapper<BaseServerInfo>().setSqlSelect("id", "serverName")));
    }

    @PostMapping("/add")
    @RequiresPermissions("basic-data:base-server-info:edit")
    public Result<Boolean> add(@RequestBody BaseServerInfoVo entity) {
        return build(iBaseServerInfoService.insert(entity.setDelFlag(DelFlagConstant.NORMAL)));
    }

    @PostMapping("/delete/batch")
    @RequiresPermissions("basic-data:base-server-info:edit")
    public Result<Boolean> add(@RequestBody List<String> ids) {
        return build(iBaseServerInfoService.deleteBatchIds(ids));
    }

    /**
     * 获取一个 Service，由子类实现
     * 实现这个方法可以更快的获取到 Service 以提高响应速度
     *
     * @return 服务类实现对象
     */
    @Override
    public Optional<IBaseServerInfoService> getService() {
        return Optional.of(iBaseServerInfoService);
    }

    @GetMapping("/service-num/{serviceNum}/position-id/{positionId}")
    @RequiresPermissions("basic-data:base-server-info")
    public Result<Boolean> selectServiceNumByPosition(@PathVariable("serviceNum") String serviceNum, @PathVariable("positionId") Long positionId) {
        return success(!iBaseServerInfoService.exists(new BaseServerInfo().setServerNumber(serviceNum).setPosition(positionId)));
    }

    @Override
    @RequiresPermissions("basic-data:base-server-info:edit")
    public Result<Boolean> update(@RequestBody BaseServerInfo entity) {
        return success(iBaseServerInfoService.updateAllColumnById(DealObjectNullValue.setNullValue(entity.setDelFlag(DelFlagConstant.NORMAL))));
    }
}

